package cn.cmcc.intertelecom.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmcc.intertelecom.bean.Power;
import cn.cmcc.intertelecom.bean.PowerGroup;
import cn.cmcc.intertelecom.bean.PowerGroupExample;
import cn.cmcc.intertelecom.bean.PowerGroupExample.Criteria;
import cn.cmcc.intertelecom.bean.RoleGroup;
import cn.cmcc.intertelecom.dao.PowerGroupMapper;
import cn.cmcc.intertelecom.dao.PowerMapper;

@Service
public class PowerGroupService {
	@Autowired
	private PowerGroupMapper powerGroupMapper;
	
	
	
	/**
	 * 根据角色的id 批量删除多条记录
	 * @param ids
	 */
	public void deleteBatchByRoleIds(List<Integer> ids) {
		//删除powerGroupMapper中的该角色的记录
		PowerGroupExample example1=new PowerGroupExample();
		Criteria criteria1 = example1.createCriteria();
		criteria1.andRoleIdIn(ids);
		powerGroupMapper.deleteByExample(example1);
	}
	
	
	/**
	 * 根据角色的id 删除一条 记录
	 * @param parseInt
	 */
	public void deleteByRoleId(int id) {
		//删除powerGroupMapper中的该角色的记录
		PowerGroupExample example=new PowerGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(id);
		powerGroupMapper.deleteByExample(example);
		
	}

	/**
	 * 配置角色的权限
	 * @param powerGroup 
	 * @param roleGroup
	 */
	public void configPower(PowerGroup powerGroup) {
		
		
		//删除该角色的所有权限
		PowerGroupExample example=new PowerGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(powerGroup.getRoleId());
		powerGroupMapper.deleteByExample(example);
		
		//配置新的权限
		powerGroup.setCreatedate(new Date());
		for (int i = 0; i < powerGroup.getPowers().length; i++) {
			powerGroup.setPower(powerGroup.getPowers()[i]);
			powerGroupMapper.insert(powerGroup);
		}

	}


	public List<PowerGroup> selectPowersByRoles(List<Integer>roleIds) {
		
		PowerGroupExample example=new PowerGroupExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdIn(roleIds);
		return powerGroupMapper.selectByExample(example);
	}

	
}
